package com.hao.crm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hao.crm.domain.Permission;
import com.hao.crm.domain.Role;
import com.hao.crm.mapper.RoleMapper;
import com.hao.crm.page.PageResult;
import com.hao.crm.query.QueryObject;
import com.hao.crm.service.IRoleService;
@Service
public class RoleServiceImpl implements IRoleService{
	@Autowired
	private RoleMapper dao;
	@Override
	public int delete(Long id) {
		return dao.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(Role e) {
		int effectCount = dao.insert(e);
		//处理角色和权限的关系
		List<Permission> permissions = e.getPermissions();
		if(permissions!=null) {
			for (Permission permission : permissions) {
				dao.handlerRelation(e.getId(),permission.getId());
			}
		}
		return effectCount;
	}

	@Override
	public Role get(Long id) {
		return dao.selectByPrimaryKey(id);
	}

	@Override
	public List<Role> selectAll() {
		return dao.selectAll();
	}

	@Override
	public int update(Role e) {
		int effectCount = dao.updateByPrimaryKey(e);
		dao.deleteByRid(e.getId());
		List<Permission> permissions = e.getPermissions();
		if(permissions!=null) {
			for (Permission permission : permissions) {
				dao.handlerRelation(e.getId(),permission.getId());
			}
		}
		return effectCount;
	}

	@Override
	public PageResult selectByCondition(QueryObject qo) {
		Long count = dao.queryByConditionCount(qo);
		if(count>0) {
			List<Role> emps = dao.queryByCondition(qo);
			return new PageResult(count, emps);
		}
		return PageResult.Empty;
	}

	@Override
	public List<Long> selectByEid(Long id) {
		return dao.selectByEid(id);
	}

	
	
}
